package com.example.reactor.r2dbc.db.template;

import com.example.reactor.r2dbc.db.Customer;
import jakarta.annotation.Resource;
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
import org.springframework.data.relational.core.query.Query;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

import static org.springframework.data.relational.core.query.Criteria.where;

/**
 * @author zpMeng```
 * @since 6/16/2025
 */
@Service
public class DynamicTableByTemplate {

    @Resource
    private R2dbcEntityTemplate template;

    public Flux<Customer> findByFirstName(String nameFilter) {
        return template
                .select(Customer.class)
                .from("customer_2") // 动态表名
                .matching(Query.query(
                        where("first_name")
                                .like(nameFilter + "%")
                ))
                .all();
    }

}
